package jwf.http;

import java.util.HashMap;
import java.util.Map;

public class Cookie {

    private Map<String, Item> cookies;

    public Cookie(String text) {
	cookies = new HashMap<String, Item>();
	parse(text);
    }

    public Item get(String name) {
	return cookies.get(name);
    }

    public Map<String, Item> getItems() {
	return cookies;
    }

    private boolean parse(String text) {
	String[] splits = text.split("; ");
	for (String split : splits) {
	    int index = split.indexOf("=");
	    if (index != -1) {
		String name = split.substring(0, index);
		String value = split.substring(index + 1);
		add(name, value);
	    }
	}
	return true;
    }

    private void add(String name, String value) {
	Item item = new Item();
	item.setName(name);
	item.setValue(value);
	cookies.put(name, item);
    }

    public void addItems(Map<String, Item> items) {
	cookies.putAll(items);
    }

}
